﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.Data;
using COM.Service;
using COM.Web;
using SampleApp.Domain;
using SampleApp.Service;


namespace SampleApp.Web.SysModule.Controllers
{
    public class SysDeptController : Controller
    {
        private ISysDeptService sysDeptService = Unity.Instance.GetService<ISysDeptService>();
        private IPermissionService permissionService = Unity.Instance.GetService<IPermissionService>();

        public ActionResult List(int page = 1)
        {
            //this.permissionService.AllowOperateOrLogin(EnumPermissionNameWeb.SampleAdmin);
            var list = this.sysDeptService.GetSysDeptList();
            return View(list);
        }

        public ActionResult Edit(string id)
        {
            return RedirectToAction(EnumOperation.Update, new { id = id });
        }

        public ViewResult Update(string id)
        {
            var sysDept = this.sysDeptService.GetSysDeptByCode(id);
            return View("Edit", sysDept);
        }

        public ActionResult Create()
        {
            var sysDept = new SysDept()
            {
                DeptCode = Guid.NewGuid().ToString(),
                DeptName = "新部门",
            };

            return View("Edit",sysDept);
        }


        [HttpPost]
        public ActionResult Create(SysDept sysDept)
        {
            if (ModelState.IsValid)
            {
                this.sysDeptService.CreateSysDept(sysDept);
                return RedirectToAction("List");
            }

            return null;
        }


        [HttpPost]
        public ActionResult Update(SysDept sysDept)
        {
            if (ModelState.IsValid)
            {
                this.sysDeptService.UpdateSysDept(sysDept);
                return RedirectToAction("List");
            }
            return null;
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            this.sysDeptService.DeleteSysDept(id);
            return null;
        }

    }
}